﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using NGI_Mensaje;
using NGI_Enumerador;
using NGI_Auxiliares;
using NGI_Errores;

namespace NGI_BaseDatos
{
    public class BaseDatos
    {
        private static SqlConnection DBConnection;
        private static String ConnectionString = "";

        public static void SetearConnectionString(String ConnectionString)
        {
            BaseDatos.ConnectionString = ConnectionString;
        }

        public static DataTable Traer(String StoredProcedure)
        {
            return BaseDatos.Traer(StoredProcedure, null);
        }

        public static DataTable Traer(String StoredProcedure, Parametro[] Parametros)
        {
            DataTable dt = new DataTable();

            try
            {
                BaseDatos.Conectar();
                SqlDataAdapter Adaptador = new SqlDataAdapter(StoredProcedure, BaseDatos.DBConnection);
                Adaptador.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (Parametros != null)
                {
                    foreach (Parametro Param in Parametros)
                    {
                        Adaptador.SelectCommand.Parameters.Add(new SqlParameter(Param.Nombre, Param.Valor));
                    }
                }

                Adaptador.Fill(dt);
            }catch(Exception Ex){
                Errores.LoguearError(Ex, "BaseDatos.Traer");
            }
            return dt;
        }

        public static Object Ejecutar(String StoredProcedure, Parametro[] Parametros)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = StoredProcedure;
                cmd.Connection = BaseDatos.DBConnection;
                cmd.CommandType = CommandType.StoredProcedure;
                BaseDatos.Conectar();
                if (Parametros != null)
                {
                    foreach (Parametro Param in Parametros)
                    {
                        cmd.Parameters.Add(new SqlParameter(Param.Nombre, Param.Valor));
                    }
                }

                Object result = cmd.ExecuteScalar();
                return result;
            }
            catch (Exception Ex)
            {
                Mensaje.MostrarMensaje(TipoMensajes.Otro, TipoCarteles.Error, TipoBotones.OK);
                Errores.LoguearError(Ex, "BaseDatos.Ejecutar");                
            }

            return new Object();

        }

        private static void Conectar(){
            try
            {
                DBConnection = new SqlConnection();
                DBConnection.ConnectionString = BaseDatos.ConnectionString;
                DBConnection.Open();
            }catch(Exception Ex){
                Mensaje.MostrarMensaje(TipoMensajes.Otro, TipoCarteles.Error, TipoBotones.OK);
                Errores.LoguearError(Ex, "BaseDatos.Conectar");
            }
        }
    }
}
